from selenium import webdriver

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import UnexpectedAlertPresentException
from selenium.webdriver.chrome.options import Options
from time import sleep
import datetime

from entity.Base import Base
from utils.GeetestSpider import GeetestSpider

class Taobao(Base):
    loginName = 'luoluqi00'
    secret = '3*SECRET'
    url = 'https://xueerqin.net'
    maotai = 'https://chaoshi.detail.tmall.com/item.htm?spm=a3204.14194314.9341139540.1.80a8c6c0BChPQD&id=20739895092'
    
    # maotai = 'https://chaoshi.detail.tmall.com/item.htm?spm=a3204.12652450.6618868352.1.c92578ddgREq7N&pos=1&acm=lb-zebra-464492-6067012.1003.1.5518797&id=559835712619&scm=1003.1.lb-zebra-464492-6067012.FF-hyhsfZA-725677994_B-350100_C-None_D-559835712619_E-D_G-89.9_X-TmcsSFlog-FF_559835712619_5518797'

    def __init__(self):
        Base.__init__(self, isProxy=False)
        
       
        # self.driver.implicitly_wait(5)

    def isElementExist(self,selector):
        try:
            ele = self.driver.find_element_by_css_selector(selector)
            return True
        except:
            return False

    def getElement(self, selector):
        # while True:
        #     print('getElement:' + selector)
        #     if self.isElementExist(selector):
        #         break
        #     sleep(0.001)
        return self.driver.find_element_by_css_selector(selector)

    def login(self):
        self.driver.get(self.url)
        # self.driver.find_element_by_link_text("亲，请登录").click()
      
        self.getElement('#uname').send_keys(self.loginName)
        self.getElement('#upass').send_keys(self.secret)
        self.getElement('#loginSubmit').click()

       
        crack = GeetestSpider(self.driver)
        crack.crack()

    def start(self):

        self.driver.get(self.maotai)
        
        # J_LinkBuy = self.getElement('#J_LinkBuy')
        # print(J_LinkBuy.is_displayed())
        # print(J_LinkBuy.is_enabled())
        # J_LinkBuy.click()
        
        J_LinkBasket =  self.getElement('#J_LinkBasket')
        while True:
            display = J_LinkBasket.is_displayed()
            if display:
                break
            sleep(0.001)
            print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'), '添加购物车的按钮还没出现')

        J_LinkBasket.click()
        js = """ document.querySelector(".tm-mcListBox").style.overflow = "visible" """
        self.driver.execute_script(js)
        
        self.getElement('.tm-mcCartBtn').click()

        handles = self.driver.window_handles
           
        self.driver.switch_to.window(handles[1])
        
        self.getElement('#J_Go').click()

        self.getElement("[title='提交订单']").click()

        self.driver.execute_script(r"document.getElementById('payPassword_rsainput').value = '111111'")

        self.getElement('#J_authSubmit').click()
        

if __name__ == "__main__":
    a = Taobao()
    a.login()
    # a.start()
    